package club.itstudio.itouc.controller.jw.stu

import club.itstudio.itouc.core.util.Result
import club.itstudio.itouc.core.util.ResultUtil
import club.itstudio.itouc.core.util.mapper
import club.itstudio.itouc.jw.ParamsBuilder
import club.itstudio.itouc.jw.with
import club.itstudio.itouc.service.JwOps
import com.fasterxml.jackson.module.kotlin.readValue
import org.springframework.web.bind.annotation.CookieValue
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 学生选课投币情况
 */
@RestController
class CoinStatus (val jwOps: JwOps,
                  val jw: ParamsBuilder){

    @GetMapping("/jw/stu/coins")
    fun coinStatus(@CookieValue token: String): Result<Any> {
        val session = jwOps.getValidJwSession(token)
        val data = jw.coinStatus(session.userCode) with session
        val resultJson:Map<String, String> = mapper.readValue(data)
        val result:Map<String, String> = mapper.readValue(resultJson["result"] ?: "")
        val resultContent = mapOf(
                "total" to result["point_total"],
                "used" to result["point_used"],
                "rest" to result["point_canused"]
        )
        return ResultUtil.success(resultContent)
    }
}